﻿using netDxf;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Tables;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.WebSockets;
using System.Windows.Media.Media3D;

namespace TestDxfDocument

{

    class drilling_bit
    {
        public static void Integratedcompositebit(Dictionary<string, Object> data)//钻头
        {
            DxfDocument dxf = new DxfDocument();
            dxf.Layers["0"].Lineweight = Lineweight.W25;
            Layer contour = new Layer("contour");//轮廓层
            Layer dimensioning = new Layer("dimensioning");//尺寸层
            Layer refin = new Layer("refin");//基准线/内轮廓/虚线
            Layer center = new Layer("center");//中心线
            Layer textlayer = new Layer("textlayer");//文本层
            Layer hatching = new Layer("hatching");//填充层
            Layer additionalline = new Layer("additionalline");//附加线
            Layer cutting = new Layer("cutting");//切削层
            fun.SetLayer(dxf);

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);
            /*
            double scale = 4;
            double d1 = 7 * scale;
            double d2 = 7 * scale;
            double l1 = 55 * scale;
            double l2 = 70 * scale;
            double ls = 15 * scale;*/
            double scale = 4;
            double d1 = Convert.ToDouble(data["dc"]) * scale;
            double d2 = Convert.ToDouble(data["dconms"]) * scale;
            double l1 = Convert.ToDouble(data["lu"]) * scale;
            double l2 = Convert.ToDouble(data["lpr"]) * scale;
            double ls = Convert.ToDouble(data["ls"]) * scale;
            double l = (d1 / 10.0) / Math.Tan(80 * pi / 180) + (d1 / 10.0) / Math.Tan(40 * pi / 180);
            Vector2 p0 = new Vector2(0, 0);
            Vector2 p0_1 = fun.Polar(p0, pi, 5);
            Vector2 p0_2 = fun.Polar(p0, 0, l2 + ls + 5);
            Vector2 p1 = new Vector2((l2 - l1) / 20.0, d1 / 2.0);
            Vector2 p2 = fun.mirrorpoint(p1, p0_1, p0_2);
            Vector2 p3_0 = fun.Polar(p0, pi / -3, d1);
            Vector2 p3 = fun.Inters(p0, p3_0, p1, p2);
            Vector2 p4_0 = fun.Polar(p0, pi / 3, d1);
            Vector2 p4 = fun.Inters(p0, p4_0, p1, p2);
            Vector2 p5 = fun.Polar(p2, 0, l1 / 11.0);
            Vector2 p6 = fun.mirrorpoint(p5, p0_1, p0_2);
            Vector2 p7_0 = fun.Polar(p4, pi / -6, l1);
            Vector2 p7 = fun.Inters(p4, p7_0, p0_1, p0_2);
            Vector2 p8 = fun.Polar(p7, pi, (p7.X - p4.X) / 5.0);
            Vector2 p9 = fun.Polar(p8, pi / -2, fun.CalDistance(p3, p4) / 5.0);
            Vector2 p10 = fun.Polar(p9, pi - 10 * pi / 180, (p7.X - p4.X) / 3.0);
            Vector2 p11_1 = fun.Polar(p10, pi / -2, l1);
            Vector2 p11_2 = fun.Polar(p3, 0, l1);
            Vector2 p11 = fun.Inters(p3, p11_2, p10, p11_1);
            Vector2 p12 = new Vector2(p1.X + p1.X / 1.5, p4.Y + (p1.Y - p4.Y) / 1.5);
            Vector2 p13 = fun.mirrorpoint(p12, p0_1, p0_2);
                Vector2 p14 = fun.Polar(p13, pi / -2 + (30 * pi / 180), ((d1 / 10.0)) / Math.Sin(80 * pi / 180));
                Vector2 p15 = fun.Polar(p14, pi - 10 * pi / 180 + (30 * pi / 180), l);
                Vector2 p16 = fun.Polar(p15, 70 * pi / 180 + (30 * pi / 180), l);
                Vector2 p17 = fun.Polar(p16, 50 * pi / 180 + (30 * pi / 180), l);
                Vector2 p18 = fun.Polar(p17, -50 * pi / 180 + (30 * pi / 180), l);
                Vector2 p19 = fun.Polar(p18, -70 * pi / 180 + (30 * pi / 180), l);
            Vector2 p20 = fun.Polar(p12, pi / -2 + (30 * pi / 180), ((d1 / 10.0)) / Math.Sin(80 * pi / 180));
            Vector2 p21 = fun.Polar(p20, pi - 10 * pi / 180 + (30 * pi / 180), l);
            Vector2 p22=fun .Polar (p21, 70 * pi / 180 + (30 * pi / 180), l); 
            Vector2 p23=fun .Polar (p22, 50 * pi / 180 + (30 * pi / 180), l);
            Vector2 p24=fun .Polar (p23, -50 * pi / 180 + (30 * pi / 180), l);
            Vector2 p25 = fun.Polar(p24, -70 * pi / 180 + (30 * pi / 180), l);
            Vector2 p26 = fun.Inters(p1, p6, p23, p24);
            Vector2 p27 = fun.Inters(p21, p20, p1, p2);
            Vector2 p28 = fun.Inters (p1, p2, p17, p18);
            Vector2 p29 = fun.Inters(p2, p5, p15, p14);
            Vector2 p30 = fun.Polar(p0, 0, l2 / 2.3);
            Vector2 p31 = fun.Polar(p0, 0, l2 / 2.8);
            Vector2 p32 = new Vector2(l2 + ls, 0);
            Vector2 p33 = fun.Polar(p32, pi / 2, d2 / 2.0-d2/10.0);
            Vector2 p34 = new Vector2(l2 + ls - d2 / 10.0, d2 / 2.0);
            Vector2 p35 = fun.Polar(p34, pi, ls / 6.0);
            Vector2 p36 = fun.Polar(p35, pi + pi / 4, fun.CalDistance(p33, p34));
            Vector2 p37 = fun.Polar(p36, pi, ls - fun.CalDistance(p33, p36) * 2 - (p33.X - p34.X));
            Vector2 p38 = fun.Polar(p37, pi - pi / 4, fun.CalDistance(p35, p36));
            Vector2 p39 = fun.Polar(p38, pi, fun.CalDistance(p34, p35));
            Vector2 p40 = fun.Polar(p39, pi, p33.X - p34.X);
            Vector2 p41 = new Vector2(p40.X, d2/2.0 + (d1+(d1/10.0) - d2) / 4.0);
            Vector2 p42 = fun.Polar(p41, pi, l2 * (0.4 / 17.5));
            Vector2 p43 = new Vector2(p42.X, (d1+d1/10.0) / 2.0);
            Vector2 p44_0 = fun.Polar(p43, pi + 15 * pi / 180, l1);
            Vector2 p44_1 = fun.Polar(p0, pi / 2, d1 / 2.0);
            Vector2 p44_2 = fun.Polar(p44_1, 0, l2 + ls);
            Vector2 p44 = fun.Inters(p43, p44_0, p44_1, p44_2);
            Vector2 p45 = fun.mirrorpoint(p44, p0_1, p0_2);
            Vector2 p46 = fun.mirrorpoint(p43, p0_1, p0_2);
            Vector2 p47 = fun.mirrorpoint(p42, p0_1, p0_2);
            Vector2 p48 = fun.mirrorpoint(p41, p0_1, p0_2);
            Vector2 p49 = fun.mirrorpoint(p40, p0_1, p0_2);
            Vector2 p50 = fun.mirrorpoint(p39, p0_1, p0_2);
            Vector2 p51 = fun.mirrorpoint(p34, p0_1, p0_2);
            Vector2 p52 = fun.mirrorpoint(p33, p0_1, p0_2);
            Vector2 p53 = fun.Polar(p44, pi, l2 * (225 / 340.0));
            Vector2 p54_1 = fun.Polar(p53, pi / 2, 0.1);
            Vector2 p54_2 = fun.Polar(p54_1, pi, l2);
            Vector2 p53_0 = fun.Polar(p53, pi + 10 * pi / 180, l2);
            Arc arc1 = fun.DrawArc5(p30, new Line(p54_1, p54_2), p6);
            Vector2 p54 = fun.Inters1(p53, p53_0, arc1, 1);
            Arc arc2 = fun.DrawArc5(p53, new Line(p54_1, p54_2), p54);
            Vector2 p55_1 = fun.mirrorpoint(p54_1, p0_1, p0_2);
            Vector2 p55_2 = fun.mirrorpoint(p54_2, p0_1, p0_2);
            Vector2 p55 = fun.mirrorpoint(p53, p0_1, p0_2);
            Arc arc3 = fun.DrawArc5(p7, new Line(p55_1, p55_2), p55);
            Vector2 p56_0 = fun.Polar(p5, 10 * pi / 180, l2);
            Vector2 p56 = fun.Inters1(p5, p56_0, arc3, 0);
            Arc arc4 = fun.DrawArc5(p5, new Line(p55_1, p55_2), p56);
            Line line1 = new Line(p44, p53);
            Line line2 = new Line(p55, p45);
            Vector2 p57_1 = fun.Polar(p10, 0, l1);
            Vector2 p57_2 = fun.Polar(p57_1, pi / -2, l2);
            Vector2 p57 = fun.Inters(p2, p45, p57_1 , p57_2);
            Vector2 p58 = fun.Polar(p57, pi / 2, d1 / 10.0);
            Vector2 p58_1 = fun.Polar(p58, pi / -2, 0.1);
            Vector2 p58_2 = fun.Polar(p58_1, pi, l2);
            Arc arc5 = fun.DrawArc5(p31, new Line(p58_1, p58_2), p58);
            Arc arc6 = fun.DrawArc5(p31, new Line(p54_1, p54_2), p6);
            Line line3 = new Line(p58, p57);
            Vector2 p59 = fun.Polar(p45, pi / 2, fun.CalDistance(p57, p58) * 2);
            Vector2 p60 = fun.Polar(p44, pi / -2, d1 / 2.8);
            Line line4 = new Line(p53, p60);
            Vector2 p61 = fun.Polar(p58, pi / 2, fun.CalDistance(p57, p58));
            Vector2 p61_1 = fun.Polar(p61, pi / -2, 0.1);
            Vector2 p61_2 = fun.Polar(p61_1, pi, l2);
            Arc arc7 = fun.DrawArc5(p30, new Line(p61_1, p61_2), p61);
            Line line5 = new Line(p61, p59);
            Arc arc8 = fun.DrawArc5(p59, new Line(p43, p46), p60);
            Line line6 = new Line(p43, p46);

            Vector2 o1 = fun.Polar(p0, pi , l2 / 2.0);
            for (int i = 0; i < 3; i++)
            {
            Vector2 o2 = fun.Polar(o1, pi / 2 + (pi * i), d1 / 2.0);
            Vector2 o3 = fun.Polar(o2, 0 + (pi * i), d1 * (1.2 / 12.0));
            Vector2 o4 = fun.Polar(o1, pi / 2 + (pi * i), d1 / 4.0);
            Vector2 o5 = fun.Polar(o4, 0 + (pi * i), fun.CalDistance(o2, o3));
            Vector2 o6 = fun.Polar(o1, pi - 20 * pi / 180 + (pi * i), fun.CalDistance(o1, o4));
            Vector2 o7 = fun.Polar(o1, pi + (pi * i), d1 / 8.0);
            Vector2 o8 = fun.Polar(o1, pi / 2 + (pi * i), d1 / 8.0);
            Vector2 o9 = fun.Polar(o1, 0 + (pi * i), d1 / 8.0);
                Vector2 o10 = fun.Polar(o1, pi - 5*pi/180+(pi*i), d1 / 2.0);
                Arc arc11 = fun.DrawArc(o10, o1, fun.Polar(o1, pi / -2+(pi*i), d1 / 2.0));
                Arc arc12 = fun.DrawArc5(o10, new Line(o1, fun.Polar(o1, pi+(pi*i), l2)), o6);
                

            Arc arc9 = fun.DrawArc(o9, o1, o8);
            Arc arc10 = fun.DrawArc(o4, o1, o6);
            double ang1 = fun.CalAngle(o1, o8) - fun.CalAngle(o1, o9);
            double ang2 = fun.CalAngle(o1, o6) - fun.CalAngle(o1, o4);
                var polyline3 = Pus.AddPointsToLwPolyLine(o9, o8, o4, o5, o3, o2, o4, o6, o7, o1);
            polyline3.Vertexes[0].Bulge = Math.Tan((ang1) / 4);
            polyline3.Vertexes[6].Bulge = Math.Tan((ang2) / 4);

                //dxf.AddEntity(new EntityObject[] { polyline3, arc11,arc12});
                fun.drawBArc(dxf, arc11, 0.5, contour );
                fun.drawBArc(dxf, arc12, 0.5, contour);
                fun.PolyFill(dxf, polyline3, 0.5, contour);
            }
            Circle cir1 = new Circle(o1, d1 / 2.0);


            var polyline1 = Pus.AddPointsToLwPolyLine(p6,p26,p23,p22,p21,p27,p1,p26,p1, p4, p0, p3,p11,p10,p9,p8,p7,p4
                ,p0,p3,p28,p17,p16,p15,p14,p19,p18,p17,p16,p15,p29, p5);
            polyline1.Vertexes[9].Bulge = Math.Tan((33 * pi / 180) / 4);
            polyline1.Vertexes[16].Bulge = Math.Tan((33 * pi / 180) / 4);
            polyline1.Vertexes[17].Bulge = Math.Tan((33 * pi / 180) / 4);
            var polyline2 = Pus.AddPointsToLwPolyLine(p39,p50,p39, p40, p41,p48,p41, p42, p43, p44, p45, p46, p47, p48, p49, p50, p51,p34,p51, p52,
                p33, p34, p35, p36, p37, p38, p39);
            polyline2.Vertexes[2].Bulge = -Math.Tan((33 * pi / 180) / 4);
            polyline2.Vertexes[14].Bulge = -Math.Tan((33 * pi / 180) / 4);
          /*  dxf.AddEntity(new EntityObject[] { polyline1, polyline2 , arc1, arc2, arc3, arc4,arc5,arc6,arc7,arc8 ,
                line1, line2 ,line3,line4,line5,line6});*/
            fun.PolyFill(dxf, polyline1, 0.5, contour);
            fun.PolyFill(dxf, polyline2, 0.5, contour);
            fun.drawBArc(dxf, arc1, 0.5, contour);
            fun.drawBArc(dxf, arc2, 0.5, contour);
            fun.drawBArc(dxf, arc3, 0.5, contour);
            fun.drawBArc(dxf, arc4, 0.5, contour);
            fun.drawBArc(dxf, arc5, 0.5, contour);
            fun.drawBArc(dxf, arc6, 0.5, contour);
            fun.drawBArc(dxf, arc7, 0.5, contour);
            fun.drawBArc(dxf, arc8, 0.5, contour);
            fun.drawBline(dxf, line1, 0.5, contour);
            fun.drawBline(dxf, line2, 0.5, contour);
            fun.drawBline(dxf, line3, 0.5, contour);
            fun.drawBline(dxf, line4, 0.5, contour);
            fun.drawBline(dxf, line5, 0.5, contour);
            fun.drawBline(dxf, line6, 0.5, contour);

            //标注
            double offset = 10 * (l2 / 350.0);
            fun.dimrot1(dxf, p34, p51,-90, offset*1.5, 1, 1.2 * scale, 1.6 * scale, 0, "d="+d2/scale  , "", "");
            fun.dimrot1(dxf, p1, p2, 90, offset, 1, 1.2 * scale, 1.6 * scale, 0, "D="+d1/scale , "", "");
            fun.dimrot1(dxf, p10, p58, 180, offset*2, 1, 1.2 * scale, 1.6 * scale, 0, "L1="+l1/scale , "", "");
            fun.dimrot1(dxf, p0, p48, 180, offset*3, 1, 1.2 * scale, 1.6 * scale, 0, "L2="+l2/scale , "", "");
            fun.dimrot1(dxf, fun .Polar (o1,pi/-2,d1/2.0), fun .Polar (o1,pi/2,d1/2.0), 90, offset*2 , 1, 1.2 * scale, 1.6 * scale, 0, "D="+d1/scale , "", "");


            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);

            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            fun.SetLayer(dxf);
            fun.SetByLayer(dxf);


            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
             }
        }
    }
